﻿using KeepFocused;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;

namespace UnitTest
{   
    /// <summary>
    ///This is a test class for DatabaseTest and is intended
    ///to contain all DatabaseTest Unit Tests
    ///</summary>
    [TestClass()]
    public class DatabaseTest
    {
        private TestContext testContextInstance;
		//TODO: fix this later
        private const string ConnectionString = @"Data Source=D:\work\opensource\keepfocused\data\logs.sdf";

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        /// <summary>
        ///A test for Database Constructor
        ///</summary>
        [TestMethod()]
        public void DatabaseConstructorTest()
        {
            string s = @"Data Source=D:\records.sdf";
            Database db = new Database(s);
            Assert.AreEqual<string>(s, db.ConnetionString);
        }

        /// <summary>
        ///A test for EndActivity
        ///</summary>
        [TestMethod()]
        public void EndActivityTest()
        {
            Database db = new Database(ConnectionString);
            string description = string.Empty;
            int id = db.GetUnfinishedSession(out description);
            Assert.AreEqual<int>(id, 0);

            int sid = db.StartNewSession("test StartNewActivity");
            Assert.AreNotEqual<int>(sid, 0);
            int aid = db.StartNewActivity(sid, "test StartNewActivity");
            Assert.AreNotEqual<int>(aid, 0);

            System.Threading.Thread.Sleep(3000);
            KeepFocused.Model.Activity activity = db.GetActivity(sid, aid);
            Assert.IsNotNull(activity);
            Assert.AreEqual<string>("test StartNewActivity", activity.Comments); 

            db.EndActivity(sid, aid, "Aborted");
            activity = db.GetActivity(sid, aid);
            Assert.AreEqual<string>("test StartNewActivity", activity.Comments);
            Assert.AreEqual<string>("Aborted", activity.Status);

            db.EndSession(sid, "Aborted");
        }

        /// <summary>
        ///A test for EndSession
        ///</summary>
        [TestMethod()]
        public void EndSessionTest()
        {
            Database db = new Database(ConnectionString);
            int id = db.StartNewSession("test EndSession");
            System.Threading.Thread.Sleep(500);
            db.EndSession(id, "Finished");

            Dictionary<int, string> sessions = db.GetAllSessions();
            Assert.IsTrue(sessions[id] == "test EndSession");
        }

        /// <summary>
        ///A test for GetAllSessions
        ///</summary>
        [TestMethod()]
        public void GetAllSessionsTest()
        {
            Database db = new Database(ConnectionString);
            int id = db.StartNewSession("test");
            Dictionary<int, string> sessions = db.GetAllSessions();
            
            Assert.IsTrue(sessions[id] == "test");
        }

        /// <summary>
        ///A test for GetUnfinishedSession
        ///</summary>
        [TestMethod()]
        public void GetUnfinishedSessionTest()
        {
            Database db = new Database(ConnectionString);
            string description = string.Empty;
            int id = db.GetUnfinishedSession(out description);

            Dictionary<int, string> sessions = db.GetAllSessions();
            Assert.IsTrue(sessions[id] == description);
        }

        /// <summary>
        ///A test for StartNewActivity
        ///</summary>
        [TestMethod()]
        public void StartNewActivityTest()
        {
            Database db = new Database(ConnectionString);
            string description = string.Empty;
            int id = db.GetUnfinishedSession(out description);
            Assert.AreEqual<int>(id, 0);

            int sid = db.StartNewSession("test StartNewActivity");
            Assert.AreNotEqual<int>(sid, 0);
            int aid = db.StartNewActivity(sid, "test StartNewActivity");
            Assert.AreNotEqual<int>(aid, 0);

            System.Threading.Thread.Sleep(3000);
            db.EndActivity(sid, aid, "Aborted");
            db.EndSession(sid, "Aborted");
        }

        /// <summary>
        ///A test for StartNewSession
        ///</summary>
        [TestMethod()]
        public void StartNewSessionTest()
        {
            string connString = ConnectionString;
            Database target = new Database(connString);
            string sessionDesc = "coding on first feature";

            int actual;
            actual = target.StartNewSession(sessionDesc);
        }


        private int addSession(Database db, string desc)
        {
            return db.StartNewSession(desc);
        }
    }
}
